package com.rock.code.bigdata.bigdatainterface.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rock.code.base.controller.BaseController;
import com.rock.code.bigdata.bigdatainterface.domain.*;
import com.rock.code.bigdata.bigdatainterface.service.HostResourceService;
import com.rock.code.common.comm.RandomGUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author zhouhao
 * @description 查询管理对象列表，如查询所有网络设备，所有主机等等
 * network
 * host
 * database
 * middleware
 * service
 * machineroom
 * vm
 * Line
 * @ip  区域ip地址
 * @date 2018/9/4 10:43
 * @Version 1.0
 */
@Controller
@RequestMapping("/hostcollect")
public class HostResourceController extends BaseController {


    @Resource
    private HostResourceService hostResourceService;


    @ResponseBody
    @RequestMapping(value = "{ip}/getHostresourceData", method = RequestMethod.GET)
    public HashMap<String, String> getHostresourceData(@PathVariable("ip") String ip, @RequestParam("moClass") String moClass, @RequestParam("offset") String offset, @RequestParam("limit") String limit) throws Exception {
        //记录插入数据条数
        int sum = 0;
        //地区标志
        String areaFlag = null;
        if (Constants.IP_BEIJING.equals(ip)) {
            areaFlag = "100";
        } else if (Constants.IP_SHANGHAI.equals(ip)) {
            areaFlag = "101";
        } else if (Constants.IP_NANJING.equals(ip)) {
            areaFlag = "102";
        } else if (Constants.IP_TIANJIN.equals(ip)) {
            areaFlag = "103";
        }
        HashMap<String, String> map = new HashMap<>();
        String url = "http://" + ip + ":8082/query/mo/" + moClass + "?offset=" + offset + "&&limit=" + limit;
        System.out.println("请求url==========" + url);
        // 组装请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        // 接收响应
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        byte[] bytes = EntityUtils.toByteArray(entity);
        String result = new String(bytes, "utf-8");
        System.out.println("采集结果返回=====" + result);
        JSONArray jsonArray = JSONArray.parseArray(result);

        //网络资源
        if ("network".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectNetwork collectObject = new TbCollectNetwork();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("ip".equals(job.getString("name")))
                        collectObject.setIp(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("manufacturer".equals(job.getString("name")))
                        collectObject.setManufacturer(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("deviceName".equals(job.getString("name")))
                        collectObject.setDevicename(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("descr".equals(job.getString("name")))
                        collectObject.setDescr(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("systemOID".equals(job.getString("name")))
                        collectObject.setSystemoid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("deviceType".equals(job.getString("name")))
                        collectObject.setDevicetype(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));

                }
                hostResourceService.saveNetwork(collectObject);
                sum++;
            }
            map.put("请求结果", moClass + "已插入条数:" + sum);
        }
        //主机资源
        if ("host".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectHost collectObject = new TbCollectHost();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("ip".equals(job.getString("name")))
                        collectObject.setIp(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("osType".equals(job.getString("name")))
                        collectObject.setOstype(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("version".equals(job.getString("name")))
                        collectObject.setVersion(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("hardwareVendors".equals(job.getString("name")))
                        collectObject.setHardwareVendors(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("deviceId".equals(job.getString("name")))
                        collectObject.setDeviceId(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("purchasingBatches".equals(job.getString("name")))
                        collectObject.setPurchasingBatches(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));

                }
                hostResourceService.saveHost(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);


        }
        //数据库
        if ("database".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectDatabase collectObject = new TbCollectDatabase();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("ip".equals(job.getString("name")))
                        collectObject.setIp(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));
                }
                hostResourceService.saveDatabase(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);

        }
        //中间件
        if ("middleware".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectMiddleware collectObject = new TbCollectMiddleware();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("ip".equals(job.getString("name")))
                        collectObject.setIp(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));
                }
                hostResourceService.saveMiddleware(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);

        }
        //应用
        if ("service".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectService collectObject = new TbCollectService();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("ip".equals(job.getString("name")))
                        collectObject.setIp(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));
                }
                hostResourceService.saveService(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);

        }

        //机房环境
        if ("machineroom".equals(moClass)) {

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectMachineroom collectObject = new TbCollectMachineroom();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("ip".equals(job.getString("name")))
                        collectObject.setIp(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("manufacturer".equals(job.getString("name")))
                        collectObject.setManufacturer(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));
                }
                hostResourceService.saveMachineroom(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);

        }

        //虚拟化
        if ("vm".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectVm collectObject = new TbCollectVm();
                collectObject.setClassname(classname);
                collectObject.setCiid(RandomGUID.getRandomGUID());
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));

                }
                hostResourceService.saveVm(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);

        }

        //网络设备线路
        if ("Line".equals(moClass)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String classname = jsonObject.getString("className");
                JSONArray propertiesArray = jsonObject.getJSONArray("properties");
                TbCollectLine collectObject = new TbCollectLine();
                collectObject.setCiid(RandomGUID.getRandomGUID());
                collectObject.setClassname(classname);
                collectObject.setAreaflag(areaFlag);
                for (int j = 0; j < propertiesArray.size(); j++) {
                    JSONObject job = propertiesArray.getJSONObject(j);
                    if ("name".equals(job.getString("name")))
                        collectObject.setName(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("lineType".equals(job.getString("name")))
                        collectObject.setLinetype(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("upifspeed".equals(job.getString("name")))
                        collectObject.setUpifspeed(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("downifspeed".equals(job.getString("name")))
                        collectObject.setDownifspeed(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("updirection".equals(job.getString("name")))
                        collectObject.setUpdirection(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("hm_Device1".equals(job.getString("name")))
                        collectObject.setHm_device1(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("port1".equals(job.getString("name")))
                        collectObject.setPort1(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("hm_Device2".equals(job.getString("name")))
                        collectObject.setHm_device2(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("port2".equals(job.getString("name")))
                        collectObject.setPort2(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("grade".equals(job.getString("name")))
                        collectObject.setGrade(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("uuid".equals(job.getString("name")))
                        collectObject.setUuid(JSONObject.parseObject(job.getString("value")).getString("value"));
                    if ("domain".equals(job.getString("name")))
                        collectObject.setDomain(JSONObject.parseObject(job.getString("value")).getString("value"));

                }
                hostResourceService.saveLine(collectObject);
                sum++;

            }
            map.put("请求结果", moClass + "已插入条数:" + sum);

        }


        return map;
    }


}
